let game = function () {
    let config = {
        width               : 5,
        height              : 5,
        period              : 100,
        cell_size           : 20,
        keepstep            : 20,
        g_color            : ['#000',
                            '#2ECC71', // green
                            '#F1C40F', // yellow
                            '#922B21', // red
                            '#34495E', // gary
                            '#1B2631', // black 
                            '#FDFEFE', // white
                        ],
        border_color     : 0,
        default_color    : 6,
        background_color : 0,
        normal_color     : 1,
        risk_color       : 2,
        ill_color        : 3,
        doctor_color     : 6,
        border_width     : 1
    };

    let clock,model,canvas;

    function load() {
        clock = new Clock(tick, config.period);
        canvas = new CanvasView("#grid-div", config);
        // model = new Virus(config);
        model = new War(config);
        canvas.bindModel(model);
        model.init();
        canvas.setOnResizeListener(setGridSize);
        $("#start").click(start);
        $("#stop").click(stop);
        $("#reset").click(reset);
        status('#status', 'Stopped');
        canvas.display();
    }

    function reset() {
        $("#stop").attr('disabled', 'true');
        $("#start").removeAttr('disabled');
        clock.stop();
        model.init();
        status('#status', "Stopped");
        canvas.refreshGrid();
    }

    function tick() {
        model.nextGeneration();
        canvas.refreshGrid();
    }

    function start() {
        $("#start").attr('disabled', 'true');
        $("#stop").removeAttr('disabled');
        clock.start();
        status('#status', "Running");
    }

    function stop() {
        $("#stop").attr('disabled', 'true');
        $("#start").removeAttr('disabled');
        clock.stop();
        status('#status', "Stopped");
    }

    function setGridSize(width, height){
        canvas.refreshGrid();
    };

    function status(divid, status) {
        $(divid).html(status);
    };

    return {
        load: load,
        reset: reset,
        start: start,
        stop: stop
    };
}();

window.onload = game.load;
